package ru.ssau.university.web.beans;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import ru.ssau.university.ejb.api.UniversityServiceLocal;
import ru.ssau.university.persistence.entity.Admin;
import ru.ssau.university.persistence.entity.Teacher;

@ManagedBean(name = "authBean")
@SessionScoped
public class AuthBean {
	@EJB
	private UniversityServiceLocal universityService;
	
	private boolean loggedIn;
	private LoginType loginType;
	private Teacher teacher;
	private Admin admin;
	private String showName;
	
	public AuthBean() {

	}
	
	public boolean loginAsAdmin(String userName, String password) {
		admin = universityService.loginAsAdmin(userName, password);
		if (admin == null) {
			return false;
		} else {
			loggedIn  = true;
			loginType = LoginType.ADMIN; 
			showName  = admin.getUserName();
			return true;
		}
	}
	
	public boolean loginAsTeacher(Long teacherId, String password) {
		teacher = universityService.loginAsTeacher(teacherId, password);
		if (teacher == null) {
			return false;
		} else {
			loggedIn  = true;
			loginType = LoginType.TEACHER; 
			showName  = teacher.getName();
			return true;
		}
	}
	
	public String exit() {
		teacher = null;
		admin = null;
		loggedIn = false;
		return "pretty:welcome";
	}
	
	public boolean isLoggedInAsTeacher() {
		return loggedIn && loginType.equals(LoginType.TEACHER);
	}
	
	public boolean isLoggedInAsAdmin() {
		return loggedIn && loginType.equals(LoginType.ADMIN);
	}

	public boolean isLoggedIn() {
		return loggedIn;
	}

	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}

	public LoginType getLoginType() {
		return loginType;
	}

	public void setLoginType(LoginType loginType) {
		this.loginType = loginType;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public String getShowName() {
		return showName;
	}

	public void setShowName(String showName) {
		this.showName = showName;
	}

	
	
}
